package cn.admobiletop.adsuyi.adapter.gdt.listener;

import android.os.Handler;
import android.os.Looper;

import com.qq.e.ads.splash.SplashADListener;
import com.qq.e.comm.util.AdError;

import cn.admobiletop.adsuyi.ad.listener.ADSuyiSplashAdListener;
import cn.admobiletop.adsuyi.adapter.gdt.data.BaseAdInfo;

/**
 * @author ciba
 * @description 开屏广告监听
 *              需要继承 BaseAdListener<ADSuyiSplashAdListener>，实现所适配渠道的开屏广告监听回调，并在回调中调用事先set的adListener，进行相应回调
 *              需要在渠道回调中调用ADSuyi以下方法：
 *                  getAdListener().onAdReceive(this) 广告获取
 *                  getAdListener().onAdExpose(this)  广告曝光
 *                  getAdListener().onAdClose(this)   广告关闭
 *                  getAdListener().onAdSkip(this)    广告被跳过
 *                  getAdListener().onAdClick(this)   广告点击
 *                  onAdFailed(code, message)         广告失败
 * @date 2020/4/7
 */
public class SplashAdListener extends BaseAdListener<ADSuyiSplashAdListener> implements SplashADListener {
    private BaseAdInfo splashAdInfo;
    private Handler handler = new Handler(Looper.getMainLooper());

    /**
     * @param platformPosId     广告平台id
     * @param adListener        开屏监听
     */
    public SplashAdListener(String platformPosId, ADSuyiSplashAdListener adListener) {
        super(platformPosId, adListener);
    }

    /**
     * 广点通倒计时结束自动关闭或点击跳过按钮
     */
    @Override
    public void onADDismissed() {
        if (getAdListener() != null && splashAdInfo != null) {
            getAdListener().onAdClose(splashAdInfo);
        }
    }

    /**
     * 广点通广告加载失败
     * @param adError
     */
    @Override
    public void onNoAD(AdError adError) {
        onAdFailed(adError.getErrorCode(), adError.getErrorMsg());
    }

    /**
     * 广点通广告成功展示回调
     */
    @Override
    public void onADPresent() {
        if (getAdListener() != null) {
            splashAdInfo = new BaseAdInfo(getPlatformPosId());
            getAdListener().onAdReceive(splashAdInfo);
        }
    }

    /**
     * 广点通广告被点击回调
     */
    @Override
    public void onADClicked() {
        if (getAdListener() != null && splashAdInfo != null) {
            getAdListener().onAdClick(splashAdInfo);
        }
    }

    /**
     * 广点通广告倒计时剩余时长回调
     * @param millisUntilFinished
     */
    @Override
    public void onADTick(long millisUntilFinished) {}

    /**
     * 广点通广告曝光回调
     */
    @Override
    public void onADExposure() {
        if (getAdListener() != null && splashAdInfo != null) {
            getAdListener().onAdExpose(splashAdInfo);
        }
    }

    /**
     * 广点通广告加载成功回调
     * @param expireTimestamp 广告需要在expireTimestamp前展示
     */
    @Override
    public void onADLoaded(long expireTimestamp) {}

    /**
     * 广点通广告加载失败回调
     * @param code
     * @param message
     */
    @Override
    public void onAdFailed(final int code, final String message) {
        // 广点通的onAdFailed回调不是在主线程，需要转到主线程，方便开发者在回调中做一些更新ui的处理
        if (handler != null) {
            handler.post(new Runnable() {
                @Override
                public void run() {
                    SplashAdListener.super.onAdFailed(code, message);
                }
            });
        }
    }

    /**
     * ADSuyi广告释放会调用该方法
     */
    @Override
    public void release() {
        super.release();
        if (handler != null) {
            handler.removeCallbacksAndMessages(null);
            handler = null;
        }
        if (splashAdInfo != null) {
            splashAdInfo.release();
            splashAdInfo = null;
        }
    }
}
